package settings_api

import (
	"github.com/gin-gonic/gin"
	"giv-vue-blog/config"
	"giv-vue-blog/core"
	"giv-vue-blog/global"
	"giv-vue-blog/models/res"
)

// SettingsUpdateView 更新系统配置
// @Tags 系统配置
// @Summary 更新系统配置
// @Description 更新系统配置
// @Param name path string true "系统配置的一些参数"
// @Router /api/settings/{name} [put]
// @Produce json
// @Success 200 {object} res.Response{data=string}
func (SettingsApi) SettingsUpdateView(c *gin.Context) {
	var crname SettingsUri
	err := c.ShouldBindUri(&crname)
	if err != nil {
		res.FailedWithCode(res.ArgumentError, c)
		return
	}
	switch crname.Name {
	case "site":
		var info config.SiteInfo
		err := c.ShouldBindJSON(&info)
		if err != nil {
			res.FailedWithCode(res.ArgumentError, c)
			return
		}
		global.Config.SiteInfo = info
		res.OkWithData(global.Config.SiteInfo, c)
	case "email":
		var email config.Email
		err := c.ShouldBindJSON(&email)
		if err != nil {
			res.FailedWithCode(res.ArgumentError, c)
			return
		}
		global.Config.Email = email
		res.OkWithData(global.Config.Email, c)
	case "qq":
		var qq config.QQ
		err := c.ShouldBindJSON(&qq)
		if err != nil {
			res.FailedWithCode(res.ArgumentError, c)
			return
		}
		global.Config.QQ = qq
		res.OkWithData(global.Config.QQ, c)
	case "qiniu":
		var qiniu config.QiNiu
		err := c.ShouldBindJSON(&qiniu)
		if err != nil {
			res.FailedWithCode(res.ArgumentError, c)
			return
		}
		global.Config.QiNiu = qiniu
		res.OkWithData(global.Config.QiNiu, c)
	case "jwt":
		var jwt config.Jwy
		err := c.ShouldBindJSON(&jwt)
		if err != nil {
			res.FailedWithCode(res.ArgumentError, c)
			return
		}
		global.Config.Jwy = jwt
		res.OkWithData(global.Config.Jwy, c)
	default:
		res.FailedWithMsg("没有对应的配置信息", c)
		return
	}
	core.SetYaml()
}
